#include "htf_ms_secure_loader.h"
#include "../../core/htf_resource_manager.h"
#include "../../utils/htf_format_detector.h"
#include "../../utils/htf_logger.h"

htf_ms_model_t* htf_ms_load_secure_model(const char* path, const uint8_t* key, size_t key_len) {
    htf_resource_t* res = htf_load_resource_secure(path, key, key_len);
    if (!res) return NULL;

    htf_model_format_t fmt = htf_detect_format((uint8_t*)res->data, res->size);
    if (fmt == HTF_FMT_UNKNOWN) {
        htf_release_resource(res);
        return NULL;
    }

    htf_ms_model_t* model = (htf_ms_model_t*)calloc(1, sizeof(htf_ms_model_t));
    if (!model) {
        htf_release_resource(res);
        return NULL;
    }

    model->weights = (float*)res->data;
    model->weight_count = res->size / sizeof(float);
    model->format = fmt;
    model->resource = res;
    return model;
}

void htf_ms_unload_model(htf_ms_model_t* model) {
    if (model) {
        htf_release_resource(model->resource);
        free(model);
    }
}